package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionBaseMapper;
import com.ytkj.digitalConstruction.dao.project.ProjectFlowMapper;
import com.ytkj.digitalConstruction.dao.user.UserMapper;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionApproach;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionBase;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionConcentrate;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionEvent;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionPreparation;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionReview;
import com.ytkj.digitalConstruction.entity.project.ProjectFlow;
import com.ytkj.digitalConstruction.entity.role.Role;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.enums.construction.ConstructionResultEnum;
import com.ytkj.digitalConstruction.enums.construction.SpecialProcessEnum;
import com.ytkj.digitalConstruction.exception.CustomException;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.reqbean.AddCommonInfo;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.reqbean.HistoryQueryBean;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.FlowBaseInfo;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.FlowInProCount;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.PlanInfoRes;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.SuccedAboutFlowBase;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.TodayAndHistory;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;
import com.ytkj.digitalConstruction.service.project.ProjectService;
import com.ytkj.digitalConstruction.service.role.RoleService;
import com.ytkj.digitalConstruction.service.user.UserService;
import com.ytkj.digitalConstruction.socketio.ChatObj;
import com.ytkj.digitalConstruction.socketio.SendMessage;
import com.ytkj.digitalConstruction.util.WebTokenUtil;

/**
 * @ClassName: ConstructionBaseService
 * @Description: 项目下的施工流程信息类
 * @author 宁煜
 * @date 2017年12月5日 上午10:55:46
 *
 */
@Service
@Transactional
public class ConstructionBaseService {
	
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ConstructionBaseMapper constructionBaseMapper;
	
	@Autowired
	private ProjectFlowMapper projectFlowMapper;

	@Autowired
	private ConstructionFlowService constructionFlowService;
	
	@Autowired
	private UserMapper userMapper;
	

	@Autowired
	private SendNoticeService sendNoticeService;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private ConstructionPlanService constructionPlanService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	
	/**
	 * @Description: 在指定项目下创建所属的流程，流程编号为时间戳，创建成功后返回项目及流程信息 
	 * @Title: addNewFlow 
	 * @param projectId 所属的项目ID 
	 * @return FlowBaseInfo 
	 * @throws
	 */
	public WholeFlowInfo addNewFlow(ConstructionBase constructionBase, User user) {
		// 验证该项目下是否已经有今日的流程
		if(getTodayFlowByProject(constructionBase.getProjectId()).getInfoBase() != null) {
			throw new CustomException(ConstructionResultEnum.EXSIST_FLOW);
		}
		
		// 验证某项目角色下的人数是否足够
		cheackPersonNumber(constructionBase.getProjectId());
		
		// 生成constructionBase并添加
		createConstructionBase(constructionBase, user);
		constructionBase.setFlowNumber(setFlowNumber(constructionBase.getCreateDate()));
		synchronized (this) {
			while(constructionBaseMapper.checkFlowNumberRepeat(constructionBase.getFlowNumber())>0) {
				constructionBase.setFlowNumber(setFlowNumber(new Date()));
			}
		}
		constructionBaseMapper.insertSelective(constructionBase);

		// id为空时，说明增加失败
		ConstructionUtilService.isAddSuccess(constructionBase.getFlowId());
		ProjectFlow projectFlow = new ProjectFlow();
		projectFlow.setFlowId(constructionBase.getFlowId());
		projectFlow.setProjectId(constructionBase.getProjectId());
		projectFlowMapper.insertSelective(projectFlow);

		ConstructionUtilService.isAddSuccess(projectFlow.getProjectFlowId());

		// 更新进度，发送对应通知并返回项目及流程信息
		WholeFlowInfo flowInfo = processNextResult(constructionBase.getFlowId(), user, 1);
		
		return flowInfo;
	}
	
	/**
	 * @Description: 验证某项目角色下的人数是否足够
	 * @Title: cheackPersonNumber 
	 * @param @param projectId    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public void cheackPersonNumber(Integer projectId) {
		List<Integer> roleIds = sendNoticeService.allNeedRoles();
		for(Integer roleId : roleIds) {
			if(projectService.getUserIdListByprojectIdAndRoleIds(projectId, Arrays.asList(roleId)).size() == 0) {
				throw new CustomException(ConstructionResultEnum.NOT_ENOUGH_PEOPLE);
			}
		}
	}

	/**
	 * @Description: 制作ConstructionBase 
	 * @Title: createConstructionBase 
	 * @param @param projectId 项目编号 
	 * @param @return 
	 * @return ConstructionBase 
	 * @throws
	 */
	public ConstructionBase createConstructionBase(ConstructionBase constructionBase, User user) {
		patchBase(constructionBase, user);
		constructionBase.setCreateBy(user.getUserId());
		constructionBase.setCreateDate(constructionBase.getUpdateDate());
		constructionBase.setStatus(0);
		constructionBase.setFlowName(setFlowName(constructionBase.getProjectId()));
		return constructionBase;
	}

	/**
	 * @Description: 补全参数
	 * @Title: patchBase 
	 * @param @param constructionBase
	 * @param @param user 
	 * @return void
	 * @throws
	 */
	public void patchBase(ConstructionBase constructionBase, User user) {
		constructionBase.setUpdateDate(new Date());
		constructionBase.setUpdateBy(user.getUserId());
	}
	
	/**
	 * @Description: 根据项目编号查询项目名称及项目下流程数量生成流程名称 
	 * @Title: setFlowName 
	 * @param @param projectId 项目编号 
	 * @param @return 
	 * @return String 
	 * @throws
	 */
	public String setFlowName(Integer projectId) {
		// 获取项目
		FlowInProCount flowInProCount = constructionBaseMapper.countFlowByProId(projectId);
		if (flowInProCount.getProjectName() == null) {
			throw new CustomException(ConstructionResultEnum.PROJECT_INFO_ERROR);
		}
		// 流程名称格式：项目名-001
		return flowInProCount.getProjectName() + "-"
				+ String.valueOf(1000 + flowInProCount.getFlowCount() + 1).substring(1);
	}

	/**
	 * @Description: 根据项目下子流程的编号获取项目信息与对应流程进度信息 
	 * @Title: getFlowInfo 
	 * @param @param flowId 流程编号 
	 * @param @return 
	 * @return FlowBaseInfo 
	 * @throws
	 */
	public FlowBaseInfo getFlowInfo(int flowId) {
		return constructionBaseMapper.selectFlowInProByFlowId(flowId);
	}

	/**
	 * @Description: 根据时间生成需要显示的流程编号 
	 * @Title: setFlowNumber 
	 * @param @return 
	 * @return String 
	 * @throws
	 */
	public String setFlowNumber(Date date) {
		return String.valueOf(date.getTime());
	}

	/**
	 * @Description: 对进度下移发送消息，并返回查询信息,0-当前节点是最后一个节点，1-进度已调整 
	 * @Title: processNextResult 
	 * @param @param 项目子流程编号 
	 * @return void 
	 * @throws
	 */
	public WholeFlowInfo processNextResult(Integer flowId, User user, Integer typeId) {
		return processNextResult(flowId, user, typeId, null);
	}
	
	/**
	 * @Description: 对进度下移发送消息，并返回查询信息,0-当前节点是最后一个节点，1-进度已调整 
	 * @Title: processNextResult 
	 * @param @param flowId
	 * @param @param user
	 * @param @param typeId
	 * @param @param special
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo processNextResult(Integer flowId, User user, Integer typeId, SpecialProcessEnum specialProcess) {
		WholeFlowInfo flowInfo = processNext(flowId, user, specialProcess);
		if(typeId != null) {
			sendNoticeService.flowNoticeSend(typeId, flowInfo, user);
		}
		
		return flowInfo;
	}

	/**
	 * @Description: 进度下移
	 * @Title: processNext 
	 * @param @param flowId
	 * @param @param user
	 * @param @param specialProcess
	 * @param @return    设定文件 
	 * @return WholeFlowInfo    返回类型 
	 * @throws
	 */
	public WholeFlowInfo processNext(Integer flowId, User user, SpecialProcessEnum specialProcess) {
		AddCommonInfo resultCode = null;
		if(specialProcess == null) {
			resultCode = constructionFlowService.processNext(flowId);
		}else {
			resultCode = specialProcess(flowId, specialProcess);
		}
		if(resultCode.getCommonId() == 0) {
			flowIsFinished(flowId);
		}
		WholeFlowInfo flowInfo = showWholeFlowByFlowId(flowId);
		sendSocketMsg(flowInfo);
		return flowInfo;
	}
	
	/**
	 * 向前端发送消息
	 * 
	 * @param flowInfo
	 */
	@Async
	public void sendSocketMsg(WholeFlowInfo flowInfo) {
		//socket io 传回flowInfo
		if (SendMessage.isLine("000000")) {
			ChatObj data = new ChatObj();
			data.setObj(flowInfo);
			SendMessage.sendMsg("000000", data);
		}else {
			logger.error("socket io is offline...");
		}
	}

	/**
	 * @Description: 进度下移
	 * @Title: processNext 
	 * @param @param flowId
	 * @param @param user
	 * @param @return    设定文件 
	 * @return WholeFlowInfo    返回类型 
	 * @throws
	 */
	public WholeFlowInfo processNext(Integer flowId, User user) {
		return processNext(flowId, user, null);
	}
	
	/**
	 * @Description: 特殊流程的处理
	 * @Title: specialProcess 
	 * @param @param flowId
	 * @param @param specialProcessId
	 * @param @return 
	 * @return AddCommonInfo
	 * @throws
	 */
	public AddCommonInfo specialProcess(Integer flowId, SpecialProcessEnum specialProcess) {
		AddCommonInfo addCommonInfo = new AddCommonInfo();
		ConstructionBase base = null;
		ConstructionBase updateBase = new ConstructionBase();
		StringBuffer buffer = null;
		switch (specialProcess) {
		case CANCEL_CONSTRUCTION:  // 施工计划选择取消施工
			base = constructionBaseMapper.selectByPrimaryKey(flowId);
			if(base.getProcess().indexOf(",13,")>0) {
				buffer = new StringBuffer(base.getProcess().substring(0, base.getProcess().indexOf(",13,")+4));
			}else {
				buffer = new StringBuffer(base.getProcess());
				buffer.append(",");
			}
			buffer.append("10,");
			buffer.append(specialProcess.getFlowId());
			updateBase.setFlowId(flowId);
			updateBase.setProcess(buffer.toString());
			constructionBaseMapper.updateByPrimaryKeySelective(updateBase);
			addCommonInfo.setCommonId(1);
			break;
		case CONFIRM_CANCEL_CONSTRUCTION:  // 监理确认直接结束
			flowIsFinished(flowId, 3);
			addCommonInfo.setCommonId(1);
			break;
		case UPDATE_CONSTRUCTION_ALLOW:  // 施工计划修改为可以施工
			base = constructionBaseMapper.selectByPrimaryKey(flowId);
			buffer = new StringBuffer(base.getProcess().substring(0, base.getProcess().indexOf(",13,")+4));
			buffer.append(specialProcess.getFlowId());
			updateBase.setFlowId(flowId);
			updateBase.setProcess(buffer.toString());
			constructionBaseMapper.updateByPrimaryKeySelective(updateBase);
			addCommonInfo.setCommonId(1);
			break;
		case SUPERVISOR_CANCEL:
			base = constructionBaseMapper.selectByPrimaryKey(flowId);
			buffer = new StringBuffer(base.getProcess());
			buffer.append(",10,");
			buffer.append(specialProcess.getFlowId());
			updateBase.setFlowId(flowId);
			updateBase.setProcess(buffer.toString());
			constructionBaseMapper.updateByPrimaryKeySelective(updateBase);
			addCommonInfo.setCommonId(1);
		}
		return addCommonInfo;
	}
	
	/**
	 * @Description: 最终完成时设置结束时间
	 * @Title: flowIsFinished 
	 * @param @param flowId 
	 * @return void
	 * @throws
	 */
	public void flowIsFinished(Integer flowId) {
		flowIsFinished(flowId, null);
	}
	
	public void flowIsFinished(Integer flowId, Integer status) {
		ConstructionBase base = new ConstructionBase();
		base.setFlowId(flowId);
		base.setFlowDate(new Date());
		if(status != null) {
			base.setStatus(status);
		}
		constructionBaseMapper.updateByPrimaryKeySelective(base);
	}
	
	/**
	 * @Description: 施工结束
	 * @Title: endConstruction 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo endConstruction(Integer flowId, User user){
		checkProcess(flowId, 15);
		//1，施工结束
		updateConstructionStatus(flowId, 1);
		
		return processNextResult(flowId, user, 8);
	}
	
	/**
	 * @Description: 手动结束
	 * @Title: endConstructionManual 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo endConstructionManual(Integer flowId, User user){
		ConstructionBase base = getBaseInfo(flowId);
		// 判断该流程是否是当日流程
		if(!ConstructionUtilService.checkNowDate(base.getCreateDate())) {
			new CustomException(ConstructionResultEnum.DATE_NOT_TODAY);
		}
		/**
		 * 判断施工是否已经结束或者取消
		 */
		checkResult(base.getStatus());
		
		// 判断监理单位是否已经确认
		if(!base.getProcess().contains("14")) {
			new CustomException(ConstructionResultEnum.SUPERVISOR_NOT_CONFIRM);
		}
		//1，手动结束
		updateConstructionStatus(flowId, 2);
		
		return processNextResult(flowId, user, 9, SpecialProcessEnum.SUPERVISOR_CANCEL);
	}
	
	/**
	 * @Description: 允许施工
	 * @Title: cancelConstruction 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public void allowConstruction(Integer flowId) {
		updateConstructionStatus(flowId, 0);
	}
	
	/**
	 * @Description: 施工状态改变
	 * @Title: isConstruction 
	 * @param @param flowId
	 * @param @param isCustruction 
	 * @return void
	 * @throws
	 */
	public void updateConstructionStatus(Integer flowId, Integer isCustruction) {
		updateConstructionStatus(flowId, isCustruction, null);
	}
	
	/**
	 * @Description: 施工状态改变
	 * @Title: updateConstructionStatus 
	 * @param @param flowId
	 * @param @param isCustruction
	 * @param @param date    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public void updateConstructionStatus(Integer flowId, Integer isCustruction, Date date) {
		ConstructionBase base = new ConstructionBase();
		base.setFlowId(flowId);
		base.setStatus(isCustruction);
		base.setFlowDate(date);
		ConstructionUtilService.isUpdateSuccess(constructionBaseMapper.updateByPrimaryKeySelective(base));
	}
	
	/**
	 * @Description: 根据projectId查询整个流程信息，创建时间降序排列
	 * @Title: showWholeFlowByProjectId 
	 * @param @param projectId
	 * @param @return    设定文件 
	 * @return List<WholeFlowInfo>    返回类型 
	 * @throws
	 */
	public List<FlowBaseInfo> showWholeFlowByProjectId(Integer projectId){
		return constructionBaseMapper.showWholeFlowByProjectId(projectId);
	}
	
	/**
	 * @Description: 整个流程进度的全部参数
	 * @Title: showWholeFlowByFlowId 
	 * @param @param id 
	 * @param @return 
	 * @return WholeFlowInfo 
	 * @throws
	 */
	public WholeFlowInfo showWholeFlowByFlowId(Integer id) {
		return constructionBaseMapper.showWholeFlowByFlowId(id);
	}

	/**
	 * @Description: 验证操作流程是否是当前流程
	 * @Title: checkProcess 
	 * @param @param flowId
	 * @param @param processId 
	 * @return void
	 * @throws
	 */
	public void checkProcess(Integer flowId, Integer processId) {
		ConstructionBase base = getBaseInfo(flowId);
		// 判断该流程是否是当日流程
		if(!ConstructionUtilService.checkNowDate(base.getCreateDate())) {
			new CustomException(ConstructionResultEnum.DATE_NOT_TODAY);
		}
		/**
		 * 判断施工是否已经结束或者取消
		 */
		checkResult(base.getStatus());
		
		/**
		 * 判断当前节点与当前操作是否一致
		 */
		checkNowProcess(base.getProcess(), processId);
	}
	
	/**
	 * @Description: 判断施工是否已经结束或者取消，如果在则抛出异常
	 * 				 1-施工结束，2-手动结束，3-取消施工
	 * @Title: checkResult 
	 * @param @param resultStatus 
	 * @return void
	 * @throws
	 */
	public void checkResult(Integer resultStatus) {
		int result[] = {1,2,3};
		for(int i=0;i<result.length;i++) {
			if(result[i] == resultStatus) {
				throw new CustomException(ConstructionResultEnum.END_OR_CANCEL);
			}
		}
	}
	
	/**
	 * @Description: 判断当前节点与当前操作是否一致
	 * @Title: checkNowProcess 
	 * @param @param baseProcesses
	 * @param @param processId 
	 * @return void
	 * @throws
	 */
	public ConstructionBase checkNowProcess(Integer flowId, Integer processId) {
		ConstructionBase base = constructionBaseMapper.selectByPrimaryKey(flowId);
		checkNowProcess(base.getProcess(), processId);
		return base;
	}
	
	/**
	 * @Description: 判断当前节点与当前操作是否一致
	 * @Title: checkNowProcess 
	 * @param @param baseProcesses
	 * @param @param processId 
	 * @return void
	 * @throws
	 */
	public void checkNowProcess(String baseProcesses, Integer processId) {
		String[] processes = baseProcesses.split(",");
		if(!processes[processes.length-1].equals(processId.toString())){
			throw new CustomException(ConstructionResultEnum.PROCESS_CHECK_ERROR);
		}
	}

	/**
	 * @Description: 查询base基本信息
	 * @Title: getBaseInfo 
	 * @param @param flowId
	 * @param @return 
	 * @return ConstructionBase
	 * @throws
	 */
	public ConstructionBase getBaseInfo(Integer flowId) {
		return constructionBaseMapper.selectByPrimaryKey(flowId);
	}
	
	/**
	 * @Description: 监理单位是否已经确认施工计划
	 * @Title: isSupervisorConfirmation 
	 * @param @param base
	 * @param @param flowId
	 * @param @return 
	 * @return boolean
	 * @throws
	 */
	public boolean isSupervisorConfirmation(Integer flowId) {
		return isProcessOver(flowId, 14);
	}
	
	/**
	 * @Description: 是否已经安检确认
	 * @Title: isConfirmApproach 
	 * @param @param flowId
	 * @param @return 
	 * @return boolean
	 * @throws
	 */
	public boolean isConfirmApproach(Integer flowId) {
		return isProcessOver(flowId, 7);
	}
	
	/**
	 * @Description: 判断某个流程进度是否已经走完
	 * @Title: isProcessOver 
	 * @param @param flowId 项目子流程编号
	 * @param @param processId 流程进度编号
	 * @param @return  
	 * @return boolean
	 * @throws
	 */
	public boolean isProcessOver(Integer flowId, Integer processId) {
		ConstructionBase base = getBaseInfo(flowId);
		// 为空则判断施工是否结束或取消
		if(processId == null) { 
			return (base.getStatus() > 0 && base.getStatus() <= 3);
		}
		String[] processes = base.getProcess().split(",");
		for(int i=0;i<processes.length;i++) {
			if(processes[i].equals(processId.toString()) && i < processes.length-1) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * @Description: 是否施工结束
	 * @Title: isEnd 
	 * @param @param flowId
	 * @param @return 
	 * @return boolean
	 * @throws
	 */
	public boolean isEnd(Integer flowId) {
		ConstructionBase base = getBaseInfo(flowId);
		return (base.getStatus() > 0 && base.getStatus() <= 2);
	}
	
	/**
	 * @Description: 验证多个项目中是否有未完成的项目
	 * @Title: hasNotFinishByProjectIds 
	 * @param @param projectIds
	 * @param @return    设定文件 
	 * @return boolean    true-有未完成的流程， false-都已经完成
	 * @throws
	 */
	public boolean hasNotFinishedByProjectIds(List<Integer> projectIds) {
		String projects = projectIds.toString().replace(" ", "");
		projects = projects.substring(1, projects.length()-2);
		if(constructionBaseMapper.checkIsFinish(projects)>0) {
			return true;
		}
		return false;
	}
	
	/**
	 * @Description: 根据项目ID展示今日/历史流程
	 * @Title: getTodayAndHistory 
	 * @param @param projectId
	 * @param @return    设定文件 
	 * @return FlowTodayHistory    返回类型 
	 * @throws
	 */
	public TodayAndHistory<ConstructionBase> getTodayAndHistory(HistoryQueryBean historyQueryBean) {
		TodayAndHistory<ConstructionBase> flowTodayHistory = new TodayAndHistory<ConstructionBase>();
		flowTodayHistory.setTodayInfo(constructionBaseMapper.showToday(historyQueryBean.getId()));
		flowTodayHistory.setHistoryInfo(constructionBaseMapper.showHistory(historyQueryBean));
		return flowTodayHistory;
	}
	
	/**
	 * @Description: 查询指定项目下的今日流程
	 * @Title: getTodayFlowByProject 
	 * @param @param projectId
	 * @param @return    设定文件 
	 * @return ConstructionBase    返回类型 
	 * @throws
	 */
	public SuccedAboutFlowBase<ConstructionBase> getTodayFlowByProject(Integer projectId) {
		SuccedAboutFlowBase<ConstructionBase> aboutFlowBase = new SuccedAboutFlowBase<ConstructionBase>();
		aboutFlowBase.setInfoBase(constructionBaseMapper.showToday(projectId));
		if(aboutFlowBase.getInfoBase() != null) {
			aboutFlowBase.setPlanContentToday(constructionPlanService.getTodayContentByFlowId(aboutFlowBase.getInfoBase().getFlowId()));
		}
		return aboutFlowBase;
	}

	/**
	 * 检查当前用户是否为施工方角色
	 * 
	 * @param request
	 * @return
	 */
	public boolean checkExePersons(HttpServletRequest request) {
		String webToken = request.getHeader("x-access-token");
		Integer userId = (Integer) WebTokenUtil.parseWebToken(webToken, "userId");
		if(userId == null){
			return false;
		}
		
		List<Integer> roleIds = userService.findRoles(userId);
		if(roleIds.isEmpty()) {
			return false;
		}
		
		List<Role> roleList = roleService.findByIdsAndName(roleIds,"施工单位");
		if(roleList.isEmpty()) {
			return false;
		}
		return true;
	}

	/**
	 * 设置流程信息的节点操作人
	 * 
	 * @param wholeFlowInfo
	 */
	public void setOperator(WholeFlowInfo wholeFlowInfo) {
		
		//施工准备（风险）
		ConstructionPreparation preparation = wholeFlowInfo.getPreparation();
		if(preparation != null) {
			
			User user = userMapper.selectByPrimaryKey(Integer.parseInt(preparation.getOperator()));
			if(user != null) {
				preparation.setOperator(user.getName());
			}
			
		}
		//施工计划
		PlanInfoRes planInfoRes = wholeFlowInfo.getPlanInfoRes();
		if(planInfoRes != null) {
			String operatorStr = planInfoRes.getOperator();

			String[] operatorIdArr = operatorStr.split(",");
			List<String> operatorIdList = new ArrayList<>();
			operatorIdList.addAll(Arrays.asList(operatorIdArr));

			if(operatorIdList.size() == 2){
                Integer userId = Integer.parseInt(operatorIdList.get(1));
                List<Integer> roleIds = userService.findRoles(userId);
                List<Role> roleList = roleService.findByIdsAndName(roleIds, "监理单位");
                if(roleList.isEmpty()){
                    operatorIdList.remove(1);
                }
            }

			if(operatorIdList.size() > 2) {
				for (int i = 1; i < operatorIdList.size() - 1; i++) {
					//监理单位
					Integer userId = Integer.parseInt(operatorIdList.get(i));
					List<Integer> roleIds = userService.findRoles(userId);
					List<Role> roleList = roleService.findByIdsAndName(roleIds, "监理单位");
					//不是监理单位
					if (roleList.isEmpty()) {
						operatorIdList.set(i, operatorIdList.get(i + 1));
						operatorIdList.remove(i + 1);
						i--;
					}
				}

			}


			String operatorIdStr = operatorIdList.get(0);
			if(operatorIdList.size() > 1){
				for(int i = 1; i < operatorIdList.size(); i++){
					operatorIdStr = ConstructionUtilService.synthesisString(operatorIdStr,",", operatorIdList.get(i));
				}
			}
			//planInfoRes.setOperator(getOperator(planInfoRes.getOperator()));
			planInfoRes.setOperator(getOperator(operatorIdStr));
		}
		
		//施工安检集结
		ConstructionConcentrate concentrate = wholeFlowInfo.getConcentrate();
		if(concentrate != null) {
			concentrate.setOperator(getOperator(concentrate.getOperator()));		
		}
		
		//施工申请进场
		ConstructionApproach approach = wholeFlowInfo.getApproach();
		if(approach != null) {
			
			approach.setOperator(getOperator(approach.getOperator()));
		}
			
		//应急事件
		ConstructionEvent event = wholeFlowInfo.getEvent();
		if(event != null) {
			User user = userMapper.selectByPrimaryKey(event.getCreateBy());
			if(user != null) {
				event.setEvent(event.getEvent()+"--(" + user.getName() + ")");
			}
		}
			
		// 施工复查
		ConstructionReview review = wholeFlowInfo.getReview();
		if(review != null) {
			review.setOperator(getOperator(review.getOperator()));
		}
	}

	/**
	 * 获取操作人名以逗号间隔，如："张三,李四"
	 * 
	 * @param operators  用户id字符串，以逗号间隔
	 * @return
	 */
	private String getOperator(String operators) {
		String operator = "";
		List<Integer> userIdList = ConstructionUtilService.stringChangeIntegerList(operators);
		if(!userIdList.isEmpty()) {
			//List<User> userList = userMapper.selectByUserIdList(userIdList);
			List<User> userList = new ArrayList<>();
			for(Integer userId: userIdList) {
				User user = userMapper.selectByPrimaryKey(userId);
				if (user != null) {
					userList.add(user);
				}
			}
			
			for(User user : userList) {
				if(StringUtils.isEmpty(operator)) {
					operator = user.getName();
				}else {
					operator = ConstructionUtilService.synthesisString(operator,",",user.getName());
				}
			}
			
			
		}
		return operator;
	}
	
}
